﻿using System;
using System.Net;
using System.Windows;

namespace ClientPreloader
{
	public partial class MainPage
	{
		public MainPage()
		{
			// Required to initialize variables
			InitializeComponent();
			LoadClient();
		}

		void LoadClient()
		{
			XapLoader loader = new XapLoader();
			loader.XapLoaded += LoaderXapLoaded;
			loader.DownloadProgressChanged += LoaderDownloadProgressChanged;
			loader.LoadXap(new Uri("TheGameClient.xap", UriKind.Relative), "TheGameClient.LoginScreen");
		}

		void LoaderDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
		{
			prgDownloadProgress.Maximum = e.TotalBytesToReceive;
			prgDownloadProgress.Value = e.BytesReceived;
			txtDownloadProgress.Text = Math.Round(e.BytesReceived*100.0/e.TotalBytesToReceive, 1).ToString();
		}

		private void LoaderXapLoaded(object sender, XapLoaderEventArgs e)
		{
			var instance = e.Instance as UIElement;
			if (instance != null)
			{
				LayoutRoot.Children.Clear();
				LayoutRoot.Children.Add(instance);
			}
		}
	}
}